package com.bmd.backgroundmanger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {
    //当前跨域请求最大时长,这是默认1天
    private static final  long MAX_AGE=24*60*60;
    private CorsConfiguration buildConfig(){
        CorsConfiguration cors = new CorsConfiguration();
        cors.addAllowedOrigin("http://localhost:9009");//1.设置访问源地址
        cors.addAllowedOrigin("*");//1.设置访问源地址
        cors.addAllowedHeader("*");//2.设置访问源请求头
        cors.addAllowedMethod("*");//3.设置访问源请求方法
        cors.setMaxAge(MAX_AGE);
        return cors;
    }
    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource urlSource = new UrlBasedCorsConfigurationSource();
        urlSource.registerCorsConfiguration("/**",buildConfig());//4.对接口配置跨域设置
        CorsFilter corsFilter = new CorsFilter(urlSource);
        return corsFilter;
    }
}
